package com.gistmap.web;

import com.gistmap.bean.Comment;
import com.gistmap.bean.User;
import com.gistmap.service.CommentService;
import com.gistmap.service.UserService;
import com.gistmap.util.Result;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/14.
 */

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /*
        编写评论保存(保存评论-为文章用户添加消息-记录评论事件）
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
        String result = "";
        User user = (User)request.getSession().getAttribute("user");
        int uid = user.getId();
        String reply = request.getParameter("reply");
        String aid = request.getParameter("aid");
        String bcid = request.getParameter("uid");
        String atitle = request.getParameter("atitle");
        int becid = Integer.parseInt(bcid);
        Comment comment = new Comment();

        comment.setUid(uid);
        comment.setReply(reply);
        comment.setType(0);
        comment.setBcid(Integer.parseInt(bcid));
        comment.setAid(Integer.parseInt(aid));

        try{
            Map<String,Object> params = new HashedMap();
            params.put("opt",3);
            params.put("oid",uid);
            params.put("bid",becid);
            params.put("source",atitle);
            commentService.save(comment,becid,params);
            result = "1";
        }
        catch (Exception e){
            e.printStackTrace();
            result = "0";
        }

        response.getWriter().write(result);

    }

    /*
        编写回复保存
     */
    @RequestMapping("/saveAnswer")
    public void saveAnswer(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
        String result = "";
        User user = (User)request.getSession().getAttribute("user");
        int uid = user.getId();

        String reply = request.getParameter("answer");
        String aid = request.getParameter("aid");
        String pid = request.getParameter("pid");
        String bcid = request.getParameter("uid");
        String atitle = request.getParameter("atitle");
        int becid = Integer.parseInt(bcid);
        Comment comment = new Comment();

        comment.setUid(uid);
        comment.setType(1);
        comment.setPid(Integer.parseInt(pid));
        comment.setReply(reply);
        System.out.println(reply);
        comment.setAid(Integer.parseInt(aid));

        try{
            Map<String,Object> params = new HashedMap();
            params.put("opt",4);
            params.put("oid",uid);
            params.put("bid",becid);
            params.put("source",atitle);
            commentService.save(comment,becid,params);
            result = "1";
        }
        catch (Exception e){
            e.printStackTrace();
            result = "0";
        }
        response.getWriter().write(result);
    }
}
